package com.nzf.vote.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nzf.vote.entity.User;
import com.nzf.vote.entity.UserVote;
import com.nzf.vote.entity.Vote;
import com.nzf.vote.model.dto.UserVoteDto;
import com.nzf.vote.model.dto.UserVoteListDto;
import com.nzf.vote.model.vo.Results;
import com.nzf.vote.service.IUserService;
import com.nzf.vote.service.IUserVoteService;
import com.nzf.vote.service.IVoteService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nzf
 * @since 2022-07-25
 */
@CrossOrigin
@RestController
@RequestMapping("/user-vote")
public class UserVoteController {
    @Autowired
    IUserVoteService userVoteService;
    @Autowired
    IUserService userService;
    @Autowired
    IVoteService voteService;

    @GetMapping("/getUserVoteByUserId/{userId}/{pageIndex}/{size}")
    public Results getUserVoteListByUserId(@PathVariable Integer userId,@PathVariable Integer pageIndex,@PathVariable Integer size){
        Page<UserVote> userVotePage = new Page<>(pageIndex,size);
        Page<UserVote> page = new LambdaQueryChainWrapper<>(userVoteService.getBaseMapper())
                .eq(UserVote::getUserId,userId).page(userVotePage);
        return getResults(page);
    }
    @GetMapping("/getUserVoteList")
    public Results getUserVote(){
        return Results.success(userVoteService.getUserList());
    }

    @GetMapping("/getUserVoteList/{pageIndex}/{size}")
    public Results getUserVoteList(@PathVariable Integer pageIndex,@PathVariable Integer size){
        Page<UserVote> userVotePage = new Page<>(pageIndex,size);
        Page<UserVote> page = new LambdaQueryChainWrapper<>(userVoteService.getBaseMapper()).page(userVotePage);
        return getResults(page);
    }
    @GetMapping("/getUserVoteByUserId/{userId}")
    public Results getUserVoteByUserId(@PathVariable Integer userId){
        return Results.success(userVoteService.getUserVoteByUserId(userId));
    }

    @NotNull
    private Results getResults(Page<UserVote> page) {
        List<UserVoteDto> userVoteDtos = new ArrayList<>();
        UserVoteListDto voteListDto = new UserVoteListDto();
        page.getRecords().forEach(userVote -> {
            UserVoteDto userVoteDto = new UserVoteDto();
            userVoteDto.setUsername(userService.getById(userVote.getUserId()).getUsername());
            userVoteDto.setTheme(voteService.getById(userVote.getVoteId()).getTheme());
            userVoteDto.setId(userVote.getId());
            userVoteDto.setOption(userVote.getMyOption());
            userVoteDto.setCreateTime(userVote.getCreateTime());
            userVoteDto.setUserId(userVote.getUserId());
            userVoteDto.setVoteId(userVote.getVoteId());
            userVoteDtos.add(userVoteDto);
        });
        voteListDto.setUserVoteDtos(userVoteDtos);
        voteListDto.setTotal(page.getTotal());
        return Results.success(voteListDto);
    }

    @PostMapping("/insertUserVote")
    public Results insertUserVote(@RequestBody UserVote userVote){
        try {
            Integer integer = userVoteService.insertUserVote(userVote);
        }catch (Exception e){
            return new Results(500,"您已经投过票了请勿重复投票");
        }
        return Results.success();
    }

    @PutMapping("/updateUserVote")
    public Results updateUserVote(@RequestBody UserVote userVote){
        return Results.success(userVoteService.updateById(userVote));
    }
    @GetMapping("/getUserVoteById/{id}")
    public Results getUserVoteById(@PathVariable Integer id){
        return Results.success(userVoteService.getById(id));
    }

    @DeleteMapping("deleteUserVoteById/{id}")
    public Results deleteUserVoteById(@PathVariable Integer id){
        return Results.success(userVoteService.removeById(id));
    }
}
